package com.jiang.sign.Controller;

import com.alibaba.fastjson.JSONObject;
import com.jiang.sign.Service.SignService;
import com.jiang.sign.Utils.LogUtil;
import com.jiang.sign.Utils.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class SignController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SignService signService;

   //Security默认登录
    @GetMapping("demo")
   //@PreAuthorize("hasAuthority('system:dept:list')")    //设置权限  设置为test权限
   //@PreAuthorize("hasAnyAuthority('system:dept:list','admin')")  //可以传入多个权限
  //@PreAuthorize("hasRole('system:dept:list')")      //内部会自动拼接ROLE_后再去比较
  //@PreAuthorize("@ex.hasAuthority('Jiang')")
    @PreAuthorize("@hasAuthorityWithJiang('66666')")
    public String demo(){
        return "demo";
    }

    @RequestMapping(value="/login",method= RequestMethod.POST, produces = {"application/json;charset=UTF-8" })
     public JSONObject login(HttpServletRequest request){
        String logHead = "login";
        long startTime = System.currentTimeMillis();
        JSONObject responsJso = new JSONObject();
        String requestParams = "";
        try {
            requestParams = ServletUtils.receiveData(request).trim();
            if (!StringUtils.isEmpty(requestParams)) {
                JSONObject reqJso = JSONObject.parseObject(requestParams);
                responsJso = signService.login(reqJso);
            }else{
                responsJso.put("status", 1);
                responsJso.put("msg", "login入参为空");
            }
        }catch (Exception e){
            responsJso.put("status",2);
            responsJso.put("msg", "login方法错误");
        }finally {
            LogUtil.logInfo(logger, requestParams, responsJso.toJSONString().length() + "", logHead, "LOGIN",
                    startTime);
            return responsJso;
        }
    }

    @RequestMapping(value="/loginout",method= RequestMethod.POST, produces = {"application/json;charset=UTF-8" })
    public JSONObject loginout(){
        String logHead = "loginout";
        long startTime = System.currentTimeMillis();
        String requestParams = "";
        JSONObject responsJso = new JSONObject();
        try {
           responsJso = signService.loginout();
        }catch (Exception e){
            responsJso.put("status",2);
            responsJso.put("msg", "loginout方法错误");
        }finally {
            LogUtil.logInfo(logger, requestParams, responsJso.toJSONString().length() + "", logHead, "LOGINOUT",
                    startTime);
            return responsJso;
        }
    }




}
